<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>成绩信息</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/dsb.css">
    <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script>
</head>
<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand one-pan-link-mark" href="/logout/">退出登陆</a>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class=""><a href="/index" class="one-pan-link-mark">基本信息</a></li>
                <li class=""><a href="/course" class="one-pan-link-mark">选课信息</a></li>
                <li class="active"><a href="/mark" class="one-pan-link-mark">成绩信息</a></li>
                <li class=""><a href="/money" class="one-pan-link-mark">消费信息</a></li>
                <li class=""><a href="/book" class="one-pan-link-mark">图书借阅</a></li>
                <li class=""><a href="/all_books/" class="one-pan-link-mark">图书管理</a></li>
                <li class=""><a href="/course_manage" class="one-pan-link-mark">课程管理</a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h2 class="sub-header">成绩信息</h2>
            
            {% if error_message %}
            <div class="alert alert-danger">
                {{ error_message }}
            </div>
            {% endif %}
            
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>学期</th>
                        <th>成绩</th>
                        <th>状态</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% if marks %}
                        {% for mark in marks %}
                        <tr>
                            <td>{{ forloop.counter }}</td>
                            <td>{{ mark.ma_xueqi.te_name }}</td>
                            <td>{{ mark.ma_mark }}</td>
                            <td>
                                {% if mark.ma_mark >= 60 %}
                                    <span class="label label-success">及格</span>
                                {% else %}
                                    <span class="label label-danger">不及格</span>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="4" class="text-center">暂无成绩信息</td>
                        </tr>
                    {% endif %}
                    </tbody>
                </table>
            </div>
            
            {% if marks %}
            <div class="panel panel-default">
                <div class="panel-heading">成绩统计</div>
                <div class="panel-body">
                    <p>总课程数：{{ marks|length }}门</p>
                    <p>平均分：{{ avg_mark|floatformat:1 }}分</p>
                </div>
            </div>
            {% endif %}

            {% if marks %}
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">成绩趋势图</h3>
                </div>
                <div class="panel-body" style="padding: 20px;">
                    <div id="chart-container" style="width: 100%; height: 300px;"></div>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>

{% if marks %}
<script>
    console.log('初始化图表脚本...');
    
    // 检查ECharts是否加载
    if (typeof echarts === 'undefined') {
        console.error('ECharts 库未加载!');
        document.getElementById('chart-container').innerHTML = 
            '<div class="alert alert-danger" style="margin-top:100px;text-align:center;">' + 
            'ECharts 库加载失败，请检查网络连接</div>';
    } else {
        console.log('ECharts 库已加载');
        
        // 初始化图表
        try {
            var chart = echarts.init(document.getElementById('chart-container'), 'white', {renderer: 'canvas'});
            console.log('图表初始化成功');
            
            $(function() {
                // 显示加载动画
                chart.showLoading({
                    text: '数据加载中...'
                });
                console.log('显示加载动画');
                
                // 获取数据
                fetchData();
                
                // 窗口大小变化时，调整图表大小
                window.addEventListener('resize', function() {
                    chart.resize();
                });
            });
            
            function fetchData() {
                console.log('开始获取数据，请求 URL: /mark/chart/');
                $.ajax({
                    type: "GET",
                    url: "/mark/chart/",
                    dataType: 'json',
                    success: function(result) {
                        console.log('收到响应:', result);
                        
                        // 隐藏加载动画
                        chart.hideLoading();
                        
                        if (result.code === 200) {
                            // 设置图表配置
                            console.log('设置图表数据');
                            try {
                                chart.setOption(result.data);
                                console.log('图表渲染成功');
                            } catch (e) {
                                console.error('设置图表选项时出错:', e);
                                document.getElementById('chart-container').innerHTML = 
                                    '<div class="alert alert-danger" style="margin-top:100px;text-align:center;">' + 
                                    '图表渲染失败: ' + e.message + '</div>';
                            }
                        } else {
                            console.error('获取成绩数据失败:', result.msg);
                            document.getElementById('chart-container').innerHTML = 
                                '<div class="alert alert-danger" style="margin-top:100px;text-align:center;">' + 
                                '加载成绩数据失败: ' + result.msg + '</div>';
                        }
                    },
                    error: function(xhr, status, error) {
                        // 隐藏加载动画
                        chart.hideLoading();
                        
                        console.error('AJAX请求失败:', error);
                        console.error('状态码:', xhr.status);
                        console.error('响应文本:', xhr.responseText);
                        
                        document.getElementById('chart-container').innerHTML = 
                            '<div class="alert alert-danger" style="margin-top:100px;text-align:center;">' + 
                            '加载成绩数据失败，请刷新页面重试<br>错误: ' + error + '</div>';
                    }
                });
            }
        } catch (e) {
            console.error('图表初始化失败:', e);
            document.getElementById('chart-container').innerHTML = 
                '<div class="alert alert-danger" style="margin-top:100px;text-align:center;">' + 
                '图表初始化失败: ' + e.message + '</div>';
        }
    }
</script>
{% endif %}

</body>
</html>